import {_decorator,Component,Node,Input,Vec2,EventKeyboard,KeyCode,input} from "cc";
import { Plane } from "./Plane";
const { ccclass, property } = _decorator;

@ccclass("GameScene")
export class GameScene extends Component {
  @property(Node)
  public planeNode: Node = null;

  start() {
    // this.planeNode = find("plane", this.node);
  }

  onLoad() {
	input.on(Input.EventType.KEY_DOWN, this._onKeyDown, this);
    // input.on(Input.EventType.KEY_UP, this._onKeyUp, this);
  }

  private _onKeyDown(event: EventKeyboard) {
    switch (event.keyCode) {
      case KeyCode.KEY_W:
        this.planeNode.getComponent(Plane).move(new Vec2(0, 1));
        break;
      case KeyCode.KEY_S:
        this.planeNode.getComponent(Plane).move(new Vec2(0, -1));
        break;
      case KeyCode.KEY_A:
        this.planeNode.getComponent(Plane).move(new Vec2(-1, 0));
        break;
      case KeyCode.KEY_D:
        this.planeNode.getComponent(Plane).move(new Vec2(1, 0));
        break;
      default:
        break;
    }
  }

  private _onKeyUp(event: EventKeyboard) {
    switch (event.keyCode) {
      case KeyCode.KEY_W:
        this.planeNode.getComponent(Plane).move(new Vec2(0, -1));
        break;
      case KeyCode.KEY_S:
        this.planeNode.getComponent(Plane).move(new Vec2(0, 1));
        break;
      case KeyCode.KEY_A:
        this.planeNode.getComponent(Plane).move(new Vec2(1, 0));
        break;
      case KeyCode.KEY_D:
        this.planeNode.getComponent(Plane).move(new Vec2(-1, 0));
        break;
      default:
        break;
    }
  }
}
